package com.yc.tomcat.core;

import java.util.Hashtable;
import java.util.Map;

public class HttpSession implements Session {
	private String id;
	private Map<String, Object> session = new Hashtable<String, Object>();

	@Override
	public void setAttribute(String key, Object value) {
		this.session.put(key, value);
		
		// 服务器需要维护一个session列表， 定时清空过期的session
		TomcatServer.sessions.put(id, this);
	}

	@Override
	public Object getAttribute(String key) {
		if (session.containsKey(key)) {
			return session.get(key);
		}
		return null;
	}

	@Override
	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String getId() {
		return this.id;
	}

	@Override
	public void removeAttribute(String key) {
		if (!session.containsKey(key)) {
			return;
		}
		session.remove(key);
	}

	@Override
	public void clearAttribute() {
		session.clear();
	}

}
